﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;

namespace Triples {
    public class ShadeConverter : IValueConverter {

        public Color Red {
            get;
            set;
        }

        public Color Purple {
            get;
            set;
        }

        public Color Green {
            get;
            set;
        }

        public Color Background {
            get;
            set;
        }


        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            if (value is ICard) {
                ICard card = value as ICard;

                switch (card.Shade) {
                    case Shades.Empty: return GetEmptyBrush ();
                    case Shades.Shaded: return GetShadedBrush (GetColor (card));
                    case Shades.Full: return GetSolidBrush (GetColor (card));
                    default: break;
                }
            }

            return DependencyProperty.UnsetValue;
        }


        private Color GetColor(ICard card) {
            switch (card.Color) {
                case Colors.Red: return Red;
                case Colors.Purple: return Purple;
                case Colors.Green: return Green;
                default: return Background;
            }
        }


        private Brush GetEmptyBrush() {
            return new SolidColorBrush(Background);
        }

        
        private Brush GetSolidBrush(Color color) {
            return new SolidColorBrush(color);
        }


        private Brush GetShadedBrush(Color color) {
            LinearGradientBrush brush = new LinearGradientBrush();
            GradientStop stop;

            brush.MappingMode = BrushMappingMode.Absolute;
            brush.SpreadMethod = GradientSpreadMethod.Repeat;
            brush.StartPoint = new Point(0, 0);
            brush.EndPoint = new Point(10, 10);

            stop = new GradientStop ();
            stop.Color = Background;
            brush.GradientStops.Add(stop);

            stop = new GradientStop();
            stop.Color = Background;
            stop.Offset = 0.499;
            brush.GradientStops.Add(stop);

            stop = new GradientStop();
            stop.Color = color;
            stop.Offset = 0.5;
            brush.GradientStops.Add(stop);

            stop = new GradientStop();
            stop.Color = color;
            stop.Offset = 1;
            brush.GradientStops.Add(stop);

            return brush;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            throw new NotImplementedException();
        }
    }
}
